package gdr.dev.creator;

import gdr.battle.core.BattleContainer;
import gdr.battle.data.map.BattleMapUnit.MapUnitType;
import gdr.battle.data.map.*;
import gdr.battle.graphics.*;
import gdr.dev.Util;

import java.awt.Color;

public class MapCreator {

	public static BattleMap createBridgeMap(BattleContainer bc, HumanGraphicalClient client, int width, int height, int bridgeLength, int bridgeSize){
		BattleMap m = new BattleMap(width, height); 
		int x = (width - bridgeLength)/2;
		int y = (height - bridgeSize)/2;
		System.out.println(x+" "+y+" "+bridgeLength+" "+bridgeSize);	
		for (int i=0; i<m.getWidth(); i++)
			for (int j=0; j<m.getHeight(); j++)
				if (i>x && i<=x+bridgeLength && (j<y || j>=y+bridgeSize))
					m.addBattleMapUnit(createBattleMapUnitEmpty(bc, client, i, j, 0));
				else
					m.addBattleMapUnit(createBattleMapUnit(bc, client, i, j, 1));
		m.addBattleMapUnit(createBattleMapUnitGate(bc, client, 1, 2, 3));
		m.addBattleMapUnit(createBattleMapUnit(bc, client, 0, 0, 3));
		m.addBattleMapUnit(createBattleMapUnit(bc, client, 1, 0, 2));
		m.addBattleMapUnit(createBattleMapUnit(bc, client, 0, 1, 2));
		m.addBattleMapUnit(createBattleMapUnit(bc, client, 1, 1, 1));
		
		return m;
	}
	
	public static BattleMap createRandomMap(BattleContainer bc, HumanGraphicalClient client, int width, int height){
		BattleMap m = new BattleMap(width, height);
		for (int i=0; i<m.getWidth(); i++)
			for (int j=0; j<m.getHeight(); j++)
				if (Util.random(100)>85)
					m.addBattleMapUnit(createBattleMapUnitEmpty(bc, client, i, j, 0));
				else
					m.addBattleMapUnit(createBattleMapUnit(bc, client, i, j, 1));
		m.addBattleMapUnit(createBattleMapUnitGate(bc, client, 1, 2, 3));
		
		return m;
	}
	
	public static BattleMap createIslandMap(BattleContainer bc, HumanGraphicalClient client, int width, int height){
		BattleMap m = new BattleMap(width, height);
		int bordo = 2;
		for (int i=0; i<m.getWidth(); i++)
			for (int j=0; j<m.getHeight(); j++){
				int z = Math.max(width/2+height/2 - Math.abs(width/2-i) - Math.abs(height/2 -j) - bordo,1);
				z = Math.min(i, width-1-i) + Math.min(j, height-1-j);
				m.addBattleMapUnit(createBattleMapUnit(bc, client, i, j, z));
			}
		
		for (int i=0; i<bordo; i++)
			for (int j=0; j<=i; j++){
				m.addBattleMapUnit(createBattleMapUnitEmpty(bc, client, width-bordo+i, j, 0));
				m.addBattleMapUnit(createBattleMapUnitEmpty(bc, client, width-bordo+i, height-1-j, 0));
				m.addBattleMapUnit(createBattleMapUnitEmpty(bc, client, bordo-i-1, height-1-j, 0));
				m.addBattleMapUnit(createBattleMapUnitEmpty(bc, client, bordo-i-1, j, 0));
			}
		
		m.addBattleMapUnit(createBattleMapUnitGate(bc, client, 4, 4, m.getMap()[4][4].getPos().getZ()));
		return m;
	}
	
	public static BattleMapUnit createBattleMapUnit(BattleContainer bc, HumanGraphicalClient client, int x, int y, int z){
		BattleMapUnit u = new BattleMapUnit();
		u.setPos(new Position(x,y,z));
		u.setMapUnitType(MapUnitType.BASE_UNIT);
		GraphicalMapUnit gu = new GraphicalMapUnit();
		gu.setMapUnit(u);
		gu.setCoordinateCalc(client.getCoordinate());
		gu.setColor(Color.yellow.darker());
		gu.setClient(client);
		u.setGraphical(gu);
		
		return u;
	}

	public static BattleMapUnit createBattleMapUnitEmpty(BattleContainer bc, HumanGraphicalClient client, int x, int y, int z){
		BattleMapUnit u = new BattleMapUnitEmpty();
		u.setPos(new Position(x,y,z));
		u.setMapUnitType(MapUnitType.EMPTY_UNIT);
		
		GraphicalMapUnit gu = new GraphicalMapUnit();
		gu.setMapUnit(u);
		gu.setCoordinateCalc(client.getCoordinate());
		gu.setColor(Color.yellow.darker());
		gu.setClient(client);
		u.setGraphical(gu);
		
		return u;
	}
	
	public static BattleMapUnit createBattleMapUnitGate(BattleContainer bc, HumanGraphicalClient client, int x, int y, int z){
		BattleMapUnitGate u = new BattleMapUnitGate();
		u.setPos(new Position(x,y,z));
		u.setMapUnitType(MapUnitType.GATE_UNIT);

		GraphicalMapUnitGate gu = new GraphicalMapUnitGate();
		gu.setMapUnit(u);
		gu.setCoordinateCalc(client.getCoordinate());
		gu.setColor(Color.yellow.darker());
		gu.setColoreInterno(Color.white);
		gu.setClient(client);
		u.setGraphical(gu);
		return u;
	}


	
}
